package 序列化二叉树;

public class Soultion
{
    int index=-1;
    String Serialize(TreeNode root)
    {
        if(root==null)
            return "#";
        else
        {
            return root.val+","+Serialize(root.left)+","+Serialize(root.right);
        }
    }
    TreeNode Deserialize(String str)
    {
        String[] s=str.split(",");
        index++;
        //index变大，终止条件
        if(index>=s.length)
            return null;
        TreeNode node=null;
        if(!s[index].equals("#"))
        {
            node=new TreeNode(Integer.parseInt(s[index]));
            node.left=Deserialize(str);
            node.right=Deserialize(str);

        }
        return node;
    }
}
